//
//  EnterViewController.m
//  Car
//
//  Created by apple on 15/9/22.
//  Copyright (c) 2015年 朱鸿儒. All rights reserved.
//

#import "EnterViewController.h"
#import "RegisterViewController.h"
#import "EnterView.h"

@interface EnterViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) EnterView *enterView;



@end

@implementation EnterViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor clearColor];
    //  设置导航条属性
    [self navicationBarControl];

    //  登录页面
    [self MyEnterView];
}



- (void)MyEnterView {
    
    //  初始化 登录页面
    self.enterView = [[EnterView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    //  协议
    self.enterView.userNameTextfield.delegate = self;
    self.enterView.userPasswordTextfield.delegate = self;
    //  登录按钮
    [self.enterView.enterButton addTarget:self action:@selector(actionClickEnter) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.enterView];

}


#pragma mark ---  textfield 代理

//  键盘回弹
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    
    return [textField resignFirstResponder];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    NSTimeInterval animationDurantion = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDurantion];
    
     //将视图的Y坐标向上移动，以使下面腾出地方用于软键盘的显示
    self.view.frame = CGRectMake(0, -100, kWidth, kHeight);
    
    //  动画结束
    [UIView commitAnimations];
    
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    NSTimeInterval animationDurantion = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDurantion];
    
    //将视图的Y坐标向上移动，以使下面腾出地方用于软键盘的显示
    self.view.frame = CGRectMake(0, 60, kWidth, kHeight);
    
    //  动画结束
    [UIView commitAnimations];

    
    
    
}


#pragma mark ---   BarButtonItem   ---

- (void)navicationBarControl {
    
    self.navigationItem.title = @"账号登录";
    
    //  leftBarButtonItem
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(actionBack)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    //  rightBarButtonItem
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStyleDone target:self action:@selector(actionRegister)];
    
    //  导航条不透明
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationItem.rightBarButtonItem = rightItem;
    
}



//  登录按钮 事件
- (void)actionClickEnter {
 
    //  获取输入框中的信息
    NSString *userName = [self.enterView.userNameTextfield.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *password = [self.enterView.userPasswordTextfield.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"tm" object:nil userInfo:@{@"aa":userName, @"bb":password}];
    
    if (userName.length == 0 || password.length == 0) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"登录信息不完整" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
    }
    else {
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        
        NSArray *userArray = [user objectForKey:@"tm"];
        
        if ([userArray[0] isEqualToString:userName] && [userArray[1] isEqualToString:password]) {
            
#pragma mark ---   单例模式 保存当前的登录信息   ---
            
            EnterInformationHandle *handle = [EnterInformationHandle share];
            
            handle.myName = nil;
            handle.myPassword = nil;
            handle.myNumber = nil;
            
            handle.myName = userArray[3];
            
            handle.myNumber = userName;
            handle.myPassword = password;
            handle.enterOrNot = YES;
            
#pragma mark ---    ---     ---     ---     ---     ---
            
            //  把登录的用户信息存到本地
            NSUserDefaults *user911 = [NSUserDefaults standardUserDefaults];
            NSArray *array = [NSArray arrayWithObjects:userName, password,userArray[3], nil];
            
            [user911 setObject:array forKey:@"911"];
            
            NSLog(@"登录成功");
            
#pragma mark ---   登录成功 向上级页面传值 改头像 和 用户名
            [[NSNotificationCenter defaultCenter] postNotificationName:@"myPhotoAndName" object:nil userInfo:nil];
            self.tabBarController.tabBar.hidden = NO;
            [self.navigationController popToRootViewControllerAnimated:YES];
            
            
            
        }
        else {
            
            //创建提醒视图告之用户输入的信息不完整或者输入错误
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入有误，重新输入" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
            
            
        }
        
    }
    
    
    
    
    
}









- (void)actionBack {
    
    self.tabBarController.tabBar.hidden = NO;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"myPhotoAndName" object:nil userInfo:nil];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)actionRegister {
    
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    
    [self.navigationController pushViewController:registerVC animated:YES];
    

}




@end
